package com.qf.controller;

import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.feign.GoodsFeign;
import com.qf.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * restTemplate: 实现跨服务调用的对象
 */
@RestController
public class OrderController {


    @Autowired
    TestService testService;


    @RequestMapping("/testCache")
    public void testCache(){
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        testService.clearCache(1);
        System.out.println(testService.test(1));
    }

    @Autowired
    RestTemplate restTemplate;
//
//    @Autowired
    EurekaClient client;

    @Autowired
    GoodsFeign goodsFeign;

    @RequestMapping("/query/{i}")
    @HystrixCommand(fallbackMethod = "fallback")
    public String query(@PathVariable Integer i){
        //获取 qf-goods 的实例 对象
//        InstanceInfo instanceInfo = client.getNextServerFromEureka("qf-goods", false);
//        String url = instanceInfo.getHomePageUrl();
//        System.out.println(url);
        //调用 商品 模块
        //响应实体对象
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url + "query", String.class);
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://QF-GOODS/goods/query", String.class);
//        String body = responseEntity.getBody();

        String body = goodsFeign.query(i);
//        String body = goodsFeign.findDetails(18);

//        Goods goods = new Goods() ;
//        goods.setId(1);
//        goods.setName("手机");
//        goods.setPrice(5.1);
//        String body = goodsFeign.save(goods);

        return "查询订单 ------ " +body;
    }

    public String fallback(Integer i){
        System.out.println("服务降级了");
        return "服务降级了";
    }

}